# 目标文件
from openpyxl.reader.excel import load_workbook
import os

# target_file = r''
# # 资源文件
# source_file = r''
# # 新生成文件
# new_file =r''
# target_dict = {}
# target_excel = load_workbook(target_file)
# target_sheet = target_excel['主表']
# for cell in target_sheet[1]:
#     target_dict[cell.value] = cell.column_letter
#
# target_i = target_sheet.max_row + 1
#
# source_excel = load_workbook(source_file)
# source_sheet = source_excel['主表']
# source_dict = {}
# for cell in source_sheet[1]:
#     source_dict[cell.column_letter] = cell.value
#
# row_i = 1
# for row in source_sheet.iter_rows():
#     if row_i == 1:
#         row_i += 1
#         continue
#     flag = False
#     for cell in row:
#         try:
#             letter = target_dict[source_dict[cell.column_letter]]
#             target_sheet[f'{letter}{target_i}'].value = cell.value
#             flag = True
#         except BaseException as e:
#             continue
#     if flag:
#         data_letter = target_dict['数据源文件名称']
#         target_sheet[f'{data_letter}{target_i}'].value = '（省水利厅复核）2025年度储备项目_2024_12_14.xlsx'
#         target_i += 1
# target_excel.save(new_file)

def get_excel_together(target_excel,target_wb_name,source_path,source_file_name,source_wb_name):
    target_sheet = target_excel[target_wb_name]
    # 找到主表中的头
    target_dict = {}
    for cell in target_sheet[1]:
        target_dict[cell.value] = cell.column_letter

    target_i = target_sheet.max_row + 1
    # 资源文件
    source_file = fr'{source_path}\{source_file_name}'
    source_excel = load_workbook(source_file)
    print(f"文件名称->{source_file}")
    source_sheet = source_excel[source_wb_name]
    # 找到资源表的头部
    source_dict = {}
    for cell in source_sheet[1]:
        source_dict[cell.column_letter] = cell.value

    source_i = 1
    for row in source_sheet.iter_rows():
        # 第一行是头部信息 我不要
        if source_i == 1:
            source_i += 1
            continue

        # 开关只要有新增就将开关打开
        flag = False
        for cell in row:
            try:
                letter = target_dict[source_dict[cell.column_letter]]
                target_sheet[f'{letter}{target_i}'].value = cell.value
                flag = True
            except BaseException as e:
                continue

        # 如果开关打开就走下一行
        if flag:
            data_letter = target_dict['数据源文件名称']
            target_sheet[f'{data_letter}{target_i}'].value = source_file_name
            target_i += 1
    return target_excel

def get_excels_together(target_excel,target_wb_name,source_path,source_file_names,source_wb_name):
    for source_file_name in source_file_names:
        target_excel = get_excel_together(target_excel,target_wb_name,source_path,source_file_name,source_wb_name)
    return target_excel

def get_excel_names(excel_path):
    filenames = os.listdir(excel_path)
    return filenames



def get_data_together(target_excel,target_wb_name,source_path,source_wb_name):
    source_file_names = get_excel_names(source_path)
    print(source_file_names)
    return get_excels_together(target_excel,target_wb_name,source_path,source_file_names,source_wb_name)

#
# target_excel = load_workbook(rf'')
# target_excel = get_data_together(target_excel,'主表',rf'','source')
# target_excel.save(rf'')
